<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>新建月进度计划</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>月计划编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=speed_month','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=speed_month','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>月计划名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>计划月份</label>
                        <div class="layui-input-block">
                            <input type="text" name="month" id="month" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ym">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">编制人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">编制日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <?php include '../public/upinfo.php'?>
        </div>



        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>计划明细</span></div>
                        <div>
                            <button type="button" onclick="choose_speed_zong_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从总计划导入</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>编码</th>
                            <th>分部名称</th>
                            <th>分项名称</th>
                            <th>单位</th>
                            <th><text>*</text>本月计划完成量</th>
                            <th><text>*</text>本月计划工期(天)</th>
                            <th>负责人</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>


        </div>


    </div>
</form>
<script>


    $("#commentForm").validate({
        rules: {
            name: {required: true},
            month: {required: true},
            baner: {required: true},
            dt: {required: true},
        },
        messages:{
            name: {required: "请输入"},
            month: {required: "请选择"},
            baner: {required: "请输入"},
            dt: {required: "请选择"},
        }
    });


    function choose_speed_zong_list(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_speed_zong_list_page_speedmonth.php?page=speed_month&enginid='+enginid,'80%','80%');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_speed_month_add}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#number").val(data.number);
                $("#dt").val(data.dt);
                $("#baner").val(data.baner);



                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" readonly class="layui-input code"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input buname"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input xiangname"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input unit"></td>';
                listAddStr += '<td><input type="number" class="layui-input finnum"></td>';
                listAddStr += '<td><input type="number" class="layui-input duration"></td>';
                listAddStr += '<td><input type="text" class="layui-input baner"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);


                public_upload_file_load('upload_files');
                render_date();
                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            parseMoney(data);


            var validRes = $("#commentForm").valid();

            data.field.enginid = get_choose_val('enginid');
            formValid('enginid');
            if(data.field.enginid == undefined || !data.field.enginid){
                validRes = false;
            }

            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var listid = $(this).find('.code').attr('listid') || 0;
                    var code = $(this).find('.code').val();
                    var buname = $(this).find('.buname').val();
                    var xiangname = $(this).find('.xiangname').val();
                    var unit = $(this).find('.unit').val();
                    var finnum = $(this).find('.finnum').val();
                    var duration = $(this).find('.duration').val();
                    var baner = $(this).find('.baner').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!finnum){
                        throw new Error('第'+index+'行，请输入本月计划完成量');
                    }
                    if(!duration){
                        throw new Error('第'+index+'行，请输入本月计划工期');
                    }

                    var chongKey = check_chongfu(i, listid);
                    if(chongKey){
                        throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                    }

                    list.push({
                        listid:listid,
                        code:code,
                        buname:buname,
                        xiangname:xiangname,
                        unit:unit,
                        finnum:finnum,
                        duration:duration,
                        baner:baner,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;



            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            facade.ajax({url: ask_speed_month_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });


    //检测重复
    function check_chongfu(index, listid){
        try {
            $('#list_list tr').each(function (i) {
                var cur_listid = $(this).find('.code').attr('listid') || 0;
                if(index != i){
                    if(listid == cur_listid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }




    function choose_enginid_delete() {
        $("#list_list").html('');
    }

</script>
</body>
</html>
